package com.xydtech.ecard.statistics.service.impl;

import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.statistics.domain.EcardUserChecking;
import com.xydtech.ecard.statistics.domain.EcardUserCheckingSum;
import com.xydtech.ecard.statistics.mapper.EcardUserCheckingMapper;
import com.xydtech.ecard.statistics.service.IEcardUserCheckingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户考勤Service业务层处理
 *
 * @author JHON
 * @date 2022-07-22
 */
@Service
public class EcardUserCheckingServiceImpl implements IEcardUserCheckingService {
    @Autowired
    private EcardUserCheckingMapper ecardUserCheckingMapper;

    /**
     * 查询用户考勤
     *
     * @param id 用户考勤主键
     * @return 用户考勤
     */
    @Override
    public EcardUserChecking selectEcardUserCheckingById(Long id) {
        return ecardUserCheckingMapper.selectEcardUserCheckingById(id);
    }

    /**
     * 查询用户考勤列表
     *
     * @param ecardUserChecking 用户考勤
     * @return 用户考勤
     */
    @Override
    public List<EcardUserChecking> selectEcardUserCheckingList(EcardUserChecking ecardUserChecking) {
        return ecardUserCheckingMapper.selectEcardUserCheckingList(ecardUserChecking);
    }

    /**
     * 查询用户考勤汇总
     *
     * @param ecardUserChecking 用户考勤
     * @return 用户考勤集合
     */
    @Override
    public List<EcardUserCheckingSum> selectEcardUserCheckingSum(EcardUserChecking ecardUserChecking) {
        List<EcardUserCheckingSum> list = ecardUserCheckingMapper.selectEcardUserCheckingSum(ecardUserChecking);
        List<EcardUserCheckingSum> checkingList = new ArrayList<>();
        for (EcardUserCheckingSum checking : list) {
            ecardUserChecking.setUserId(checking.getUserId());
            List<EcardUserCheckingSum> infoList = ecardUserCheckingMapper.selectEcardUserCheckingInfo(ecardUserChecking);
            String checkInfo = "";
            for (EcardUserCheckingSum info : infoList) {
                checkInfo += info.getOrgName() + "：累计打卡次数 " + info.getPackNums() + "。\n";
            }
            checking.setCheckInfo(checkInfo);
            checkingList.add(checking);
        }


        return checkingList;
    }

    /**
     * 新增用户考勤
     *
     * @param ecardUserChecking 用户考勤
     * @return 结果
     */
    @Override
    public int insertEcardUserChecking(EcardUserChecking ecardUserChecking) {
        ecardUserChecking.setCreateTime(DateUtils.getNowDate());
        return ecardUserCheckingMapper.insertEcardUserChecking(ecardUserChecking);
    }

    /**
     * 修改用户考勤
     *
     * @param ecardUserChecking 用户考勤
     * @return 结果
     */
    @Override
    public int updateEcardUserChecking(EcardUserChecking ecardUserChecking) {
        return ecardUserCheckingMapper.updateEcardUserChecking(ecardUserChecking);
    }

    /**
     * 批量删除用户考勤
     *
     * @param ids 需要删除的用户考勤主键
     * @return 结果
     */
    @Override
    public int deleteEcardUserCheckingByIds(String ids) {
        return ecardUserCheckingMapper.deleteEcardUserCheckingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户考勤信息
     *
     * @param id 用户考勤主键
     * @return 结果
     */
    @Override
    public int deleteEcardUserCheckingById(Long id) {
        return ecardUserCheckingMapper.deleteEcardUserCheckingById(id);
    }
}
